﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KeepAnimals.BLL;
using KeepAnimals.UI;

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace KeepAnimals.UI
{
    public partial class FormAdoptAnimal : Form
    {
        private string Name { get; set; }
        private int Id { get; set; }
        private int Category { get; set; }

        public FormAdoptAnimal(string name, int id_animal)
        {
            Name = name;
            Id = id_animal;
        }

        public FormAdoptAnimal()
        {
            InitializeComponent();
            comboBox_civility.Items.Add("Mademoiselle");
            comboBox_civility.SelectedIndex = 0; //set the first item as select item
            comboBox_civility.Items.Add("Madame");
            comboBox_civility.Items.Add("Monsieur");


            comboBox_habitation.Items.Add("Maison");
            comboBox_habitation.SelectedIndex = 0; //set the first item as select item
            comboBox_habitation.Items.Add("Appartement");
            comboBox_habitation.Items.Add("Ferme");
            comboBox_habitation.Items.Add("Autre");

            comboBox_marital_status.Items.Add("Célibataire");
            comboBox_marital_status.SelectedIndex = 0; //set the first item as select item
            comboBox_marital_status.Items.Add("Mariée");
            comboBox_marital_status.Items.Add("Divorcée");
            comboBox_marital_status.Items.Add("Veuve");


            comboBox_animal.Items.Add("Chat");
            comboBox_animal.Items.Add("Chien");

            // we passed the second contruct to acces to geter and setter
            /* FormAdoptAnimal item = new FormAdoptAnimal("initialisation", 0);

             List<Animal> Animals = Animal.GetListOfAllAnimals();
             foreach(Animal value in Animals){
                 item.Name = value.Name;
                 item.Id = value.Id;

                 comboBox_animal.Items.Add(item.Name);
             }*/

        }

        /// <summary>
        /// Handles the SelectedValueChanged event of the comboBox_animal control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void comboBox_animal_SelectedValueChanged(object sender, EventArgs e)
        {
            comboBox_name.Items.Clear();
            int j = 0;

            // get the category for select animals in the database
            if (comboBox_animal.Text == "Chat")
            {
                Category = 0;
            }
            else if (comboBox_animal.Text == "Chien")
            {
                Category = 1;
            }


            // List of animal with categorie selected
            List<Animal> Animals = Animal.GetAnimalByIdCategory(Category);
            foreach (Animal value in Animals)
            {
                //comboBox_name.Items.Add(value.Name);
                comboBox_name.Items.Add(value.Name);


                // for default select in comboBox
                if (j == 0)
                {
                    comboBox_name.SelectedIndex = 0; //set the first item as select item
                    j++;
                }

            }
        }

        // When an animal is Selected in comboBox
        private void comboBox_name_SelectedValueChanged(object sender, EventArgs e)
        {
            //Animal animal = Animal.GetAnimalByName(comboBox_name.Text);
            Animal animal = Animal.GetAnimalByName(comboBox_name.Text);

            //this.label_departement.Text = animal.
            //this.label_departement.Visible = true;

            this.label_box.Text = animal.IdBox.ToString();
            this.label_box.Visible = true;

            this.label_age.Text = animal.Age.ToString();
            this.label_age.Visible = true;

            this.label_weight.Text = animal.Weight.ToString();
            this.label_weight.Visible = true;

            this.label_description.Text = animal.Description;
            this.label_description.Visible = true;

            if (animal.Description == "0")
            {
                this.label_atdress.Text = "Non";
            }
            else if (animal.Description == "1")
            {
                this.label_atdress.Text = "Oui";
            }
            this.label_description.Visible = true;

            this.label_reason.Text = animal.Reason;
            this.label_reason.Visible = true;

            this.label_date.Text = animal.DateArrival.ToShortDateString();
            this.label_date.Visible = true;


            //MessageBox.Show("Value : " + comboBox_name.SelectedIndex.ToString());
        }

        private void button_generate_pdf_Click(object sender, EventArgs e)
        {
            Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("Test-" + comboBox_name.Text + "-" + label_age.Text + ".pdf", FileMode.Create));
            doc.Open(); // Open the document to write

            //Create a single column table
            var t = new PdfPTable(1);

            //Tell it to fill the page horizontally
            t.WidthPercentage = 100;

            //Create a single cell
            var c = new PdfPCell();

            //Tell the cell to vertically align in the middle
            c.HorizontalAlignment = Element.ALIGN_MIDDLE;

            //Tell the cell to fill the page vertically
            c.MinimumHeight = doc.PageSize.Height - (doc.BottomMargin + doc.TopMargin);



            iTextSharp.text.Image PNG = iTextSharp.text.Image.GetInstance("picture.png");
            //PNG.ScalePercent(f); // for resize the image (25% of the image)
            doc.Add(PNG);
            //Write some content
            DateTime date_depart = new DateTime();
            Paragraph paragraph = new Paragraph(

            "\n\n Informations de proprietaire\n\n" +

            "\n Civilité : " + comboBox_civility.Text +
            "\n Prénom : " + TextBox_firstname.Text +
            "\n Nom : " + textBox_name.Text +
            "\n Telephone : " + textBox_phone_number.Text +
            "\n Adresse : " + textBox_address.Text +
            "\n Pays : " + textBox_country.Text +
            "\n Ville : " + textBox_town.Text +
            "\n Code postal : " + textBox_zipcode.Text +
            "\n Habitation : " + comboBox_habitation.Text +
            "\n Situation matrimonial: " + comboBox_marital_status.Text +

            "\n\n Informations de l'animal\n\n" +

            "\n Type de l'animal : " + comboBox_animal.Text +
            "\n Nom de l'animal : " + comboBox_name.Text +
            "\n Age de l'animal : " + label_age.Text +
            "\n Poids de l'animal : " + label_weight.Text +
            "\n Desciption de l'animal : " + label_description.Text +
            "\n L'animal est à dréssé ? " + label_atdress.Text +
            "\n Date de départ : " + date_depart.Date +
            "\n\n\n Signature : "
            );


            // Now add the above created text using different class object to our pdf document
            //doc.Add(paragraph);
            //Create a test paragraph

            //Add it a couple of times
            c.AddElement(paragraph);

            //Add the cell to the paragraph
            t.AddCell(c);

            //Add the table to the document
            doc.Add(t);

            doc.Close(); // Close document
        }


    }
}
